package com.sojson.project.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.project.controller.param.verify.AdPositionParamVerify;
import com.sojson.project.entity.po.AdPosition;
import com.sojson.project.service.AdPositionService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 广告位管理表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "广告位表管理")
@RestController
@RequestMapping(value = "/adPosition")
public class AdPositionController {

    @Resource
    private AdPositionService adPositionServiceImpl;

    /**
     * 单条插入
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/adPosition/insert')")
    @ApiOperation("添加广告位")
    public Object insert(@RequestBody AdPosition adPosition) throws SQLException {
        // 校验参数
        Result<?> paramResult = AdPositionParamVerify.insert(adPosition);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        adPosition.insertInit();
        adPositionServiceImpl.insert(adPosition);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody AdPosition adPosition) throws SQLException {
        return Result.success(adPositionServiceImpl.insertList(adPosition));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody AdPosition adPosition) throws SQLException {
        return Result.success(adPositionServiceImpl.deleteByDto(adPosition));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        int countAll = adPositionServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        List<AdPosition> findAll = adPositionServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Object countByDto(@RequestBody AdPosition adPosition) throws SQLException {
        int countByDto = adPositionServiceImpl.countByDto(adPosition);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/adPosition/findByDto')")
    @ApiOperation(value = "根据对象查询广告位", response = AdPosition.class)
    public Object findByDto(AdPosition adPosition) throws SQLException {
        List<AdPosition> findByDto = adPositionServiceImpl.findByDto(adPosition);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @DeleteMapping("/deleteById")
    @PreAuthorize("@ss.hasPermi('/adPosition/deleteById')")
    @ApiOperation("通过id删除广告位(物理删除)")
    public Object deleteById(Integer id) throws SQLException {
        adPositionServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(adPositionServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody AdPosition adPosition) throws SQLException {
        adPositionServiceImpl.updateById(adPosition);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody AdPosition adPosition) throws SQLException {
        return Result.success(adPositionServiceImpl.updateStaticByIdList(adPosition));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody AdPosition adPosition) throws SQLException {
        return Result.success(adPositionServiceImpl.updateChangeByIdList(adPosition));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/adPosition/updatePartialById')")
    @ApiOperation("通过id更新广告位(局部更新)")
    public Object updatePartialById(@RequestBody AdPosition adPosition) throws SQLException {
        // 校验参数
        Result<?> paramResult = AdPositionParamVerify.updatePartialById(adPosition);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        adPosition.updateInit();
        adPositionServiceImpl.updatePartialById(adPosition);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody AdPosition adPosition) throws SQLException {
        return Result.success(adPositionServiceImpl.updateStaticPartialByIdList(adPosition));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody AdPosition adPosition) throws SQLException {
        return Result.success(adPositionServiceImpl.updateChangePartialByIdList(adPosition));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping("/findById")
    @ApiOperation(value = "通过id查询广告位", response = AdPosition.class)
    public Object findById(Integer id) throws SQLException {
        AdPosition findById = adPositionServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<AdPosition> findByIdList = adPositionServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody AdPosition adPosition) throws SQLException {
        ArrayList<Integer> findIdByDto = adPositionServiceImpl.findIdByDto(adPosition);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        adPositionServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param adPosition
     * @return
     * @throws SQLException
     */
    @GetMapping("/findAppByDto")
    @ApiOperation(value = "根据对象查询广告位", response = AdPosition.class)
    public Object findAppByDto(AdPosition adPosition) throws SQLException {
        adPosition.setStatus(EGeneralStatus.NORMAL.getCode());
        List<AdPosition> findByDto = adPositionServiceImpl.findByDto(adPosition);
        return Result.success(findByDto, findByDto);
    }

}